//
//  DownLoadViewController.m
//  Leisure
//
//  Created by xalo on 16/7/4.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "DownLoadViewController.h"

#import <AVFoundation/AVFoundation.h>


@interface DownLoadViewController ()<UITableViewDataSource,UITableViewDelegate,playManagerDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIView *leftView;
@property (weak, nonatomic) IBOutlet UIView *middleView;
@property (weak, nonatomic) IBOutlet UIView *rightView;
@property (weak, nonatomic) IBOutlet UITableView *leftTableView;
@property (weak, nonatomic) IBOutlet UITableView *rightTableView;
//@property (nonatomic,strong)NSMutableArray *dataSource;
@property (weak, nonatomic) IBOutlet UIImageView *middleImageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (nonatomic,strong)NSTimer *timer;
@property (weak, nonatomic) IBOutlet UISlider *slider;
//当前正在播放的下标
@property (nonatomic,assign)NSInteger currentIndex;
@property (nonatomic, strong)UIView *greenView;

@end

@implementation DownLoadViewController



//定时器懒加载
-(NSTimer *)timer{
    if (!_timer) {
        _timer =[ NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    }
    return _timer;
}


-(void)timerAction{
    //detrgerwgeqw
    
    
    
}

//根据下标找回对应的cell
- (UITableViewCell *)fetchCellWithIndex:(NSInteger)index{
    
    return [self.leftTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
}

- (UIView *)greenView{
    
    if (!_greenView) {
        _greenView = [[UIView alloc] initWithFrame:CGRectMake(kWidth-10, 0, 10, [self fetchCellWithIndex:0].frame.size.height)];
        _greenView.backgroundColor = [UIColor greenColor];
    }
    return _greenView;
}

//重写datasource的方法
-(void)setDataSources:(NSArray *)dataSources{
    
    
    if (_dataSources[0] != dataSources[0]) {
        self.currentIndex = -1;
        _dataSources = dataSources;
    }
    
}


//创建单例控制器
+(instancetype)createController{
    static    DownLoadViewController *downLoadVC = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
       downLoadVC = [[DownLoadViewController alloc]initWithNibName:@"DownLoadViewController" bundle:nil];
    });
    return downLoadVC;
    
}


//-(NSMutableArray *)dataSource{
//    if (!_dataSource) {
//        _dataSource = [NSMutableArray array];
//    }
//    return _dataSource;
//}
//上一曲
- (IBAction)lastAction:(id)sender {
    
    
    if (self.currentIndex == 0  ) {
        self.currentIndex = self.dataSources.count-1;
    }else{
        self.currentIndex--;
    }
    
    
}


//播放或暂停
- (IBAction)playOrPauseAction:(id)sender {
    
    if ([[PlayerManager defaultManager]isPlaying]) {
        [[PlayerManager defaultManager]pauseMusic];
    }else{
        [[PlayerManager defaultManager]playMusic];
    }
    [self setPlayInfoWithIndex:self.index];
    
}
//下一曲

- (IBAction)nextAction:(id)sender {
    if (self.currentIndex == self.dataSources.count-1) {
        self.currentIndex = 0;
    }else{
        self.currentIndex++;
    }
    [self setPlayInfoWithIndex:self.currentIndex];
}
- (IBAction)sliderAction:(id)sender {
    [[PlayerManager defaultManager]playMusicWithSliderValue:self.slider.value];
    
}




//设置界面信息
-(void)setPlayInfoWithIndex:(NSInteger)index{
    RadioListModel *model = self.dataSources[index];
    //子线程
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
 //播放音乐
        [[PlayerManager defaultManager]prepareToPlayMusicWithURL:model.musicUrl];
        
        
        //回到主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            self.title  = model.title;
            [self.middleImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
            [self.greenView removeFromSuperview];
            [[self fetchCellWithIndex:index] addSubview:self.greenView];
            [self.leftTableView reloadData];
        });
        
    });
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.middleImageView sd_setImageWithURL:[NSURL URLWithString:self.radioListModel.coverimg]];
    self.titleLabel.text = self.model.title;
    self.navigationItem.rightBarButtonItem =  [[UIBarButtonItem alloc]initWithTitle:@"下载" style:UIBarButtonItemStylePlain target:self action:@selector(downloadAction)];
    [self.leftTableView registerNib:[UINib nibWithNibName:@"DownLoadCell" bundle:nil] forCellReuseIdentifier:@"download_cell"];
    
    
    self.currentIndex = -1;
    
    [PlayerManager defaultManager].delegate = self;
    
    //设置webView
    self.rightTableView.backgroundColor = [UIColor clearColor];
    self.rightTableView.opaque = NO;
    
    
}
//视图将要出现
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    if (self.index != self.currentIndex) {
        self.currentIndex = self.index;
        [self setPlayInfoWithIndex:self.currentIndex];
        
        
    }
    
}




-(void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    self.middleImageView.layer.cornerRadius = self.middleImageView.frame.size.width/2;
    self.middleImageView.layer.masksToBounds = YES;
    
}

-(void)downloadAction{
    
    
    
    
}


#pragma mark playManagerDelegate
-(void)playManagerDelegateFetchTotalTime:(NSString *)totalTime currentTime:(NSString *)currentTime progress:(CGFloat)progress{
    
    self.slider.value = progress;
    
    
    
    
    
}
-(void)playToNextMusic{
    [self nextAction:nil];
}



-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (self.scrollView.contentOffset.x == 0) {
        self.leftView.backgroundColor =[UIColor greenColor];
        self.middleView.backgroundColor = [UIColor whiteColor];
        self.rightView.backgroundColor = [UIColor whiteColor];
    }else if (self.scrollView.contentOffset.x == kWidth){
        self.leftView.backgroundColor =[UIColor whiteColor];
        self.middleView.backgroundColor = [UIColor greenColor];
        self.rightView.backgroundColor = [UIColor whiteColor];
    }else{
        self.leftView.backgroundColor =[UIColor whiteColor];
        self.middleView.backgroundColor = [UIColor whiteColor];
        self.rightView.backgroundColor = [UIColor greenColor];
    }
    

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    

    if (self.scrollView.contentOffset.x == 0) {
        self.leftView.backgroundColor =[UIColor greenColor];
        self.middleView.backgroundColor = [UIColor whiteColor];
        self.rightView.backgroundColor = [UIColor whiteColor];
    }else if (self.scrollView.contentOffset.x == kWidth){
        self.leftView.backgroundColor =[UIColor whiteColor];
        self.middleView.backgroundColor = [UIColor greenColor];
        self.rightView.backgroundColor = [UIColor whiteColor];
    }else{
        self.leftView.backgroundColor =[UIColor whiteColor];
        self.middleView.backgroundColor = [UIColor whiteColor];
        self.rightView.backgroundColor = [UIColor greenColor];
    }
    
    
}


#pragma mark 绘制cell

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DownLoadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"download_cell" forIndexPath:indexPath];
  
    if (tableView == self.leftView) {
//        RadioListModel *model = self.dataSource[indexPath.row];
//        [cell setCellWithModel:model];

    }
    
    return cell;
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSources.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
